#include "MemsModule.h"

//#include "ML307RDC_Module.h"
#include "GnssTau1202Module.h"
#include "UsartInitHelper.h"

#include "BufDef.h"

#include <stdbool.h>
#include <cjson.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// 是否有需要发送的数据
bool get_message;

//int x_g_val;
//int y_g_val;
//int z_g_val;
// 加速度字符串
char xa_val_str[16];
char ya_val_str[16];
char za_val_str[16];
// 速度字符串
char xs_val_str[16];
char ys_val_str[16];
char zs_val_str[16];
// 峭度字符串
char xk_val_str[16];
char yk_val_str[16];
char zk_val_str[16];


// RS485-3发送函数
void Thread_RS485_3_Usart_Send() {
    {// RS485-3发送函数
      DMA_SetSrcAddr(CM_DMA2, DMA_CH1, (uint32_t) usart_9_tx_msq_buf);

      DMA_SetTransCount(CM_DMA2, DMA_CH1, 1024);

      (void) DMA_ChCmd(CM_DMA2, DMA_CH1, ENABLE);

      USART_FuncCmd(CM_USART9, USART_TX, ENABLE);
      rt_err_t result = rt_sem_take(rs485_3_usart_tx_sem, 500);
	  SWDT_FeedDog();
    }// RS485-3发送函数
} // RS485-3发送函数

// RS485-3接收函数
bool Thread_RS485_3_Usart_Recv() {
    if (rt_mq_recv(rs485_3_usart_rx_mq, usart_9_rx_app_buf, 1024, 500) > 0) {
      GPIO_SetPins(LED_ERR_PORT, LED_ERR_PIN);
		SWDT_FeedDog();
		return false;
    }
	SWDT_FeedDog();
	return true;

} // RS485-3接收函数

// 加速度仪任务函数
void Thread_Memes_Task(void *parmameter) {
	// 是否有上报数据
	get_message = false;
	
//	rt_mq_init(&mqtt_message_tx_msq,
//             "mqtt_message_tx_msq",
//             &mqtt_message_tx_msq_pool[0],      /* 内存池指向 msg_pool */
//             1536,              /* 每个消息的大小是 1 字节 */
//             sizeof(mqtt_message_tx_msq_pool),  /* 内存池的大小是 msg_pool 的大小 */
//             RT_IPC_FLAG_PRIO); /* 如果有多个线程等待，优先级大小的方法分配消息 */
//	rt_mq_init(&mqtt_message_rx_msq,
//             "mqtt_message_rx_msq",
//             &mqtt_message_rx_msq_pool[0],      /* 内存池指向 msg_pool */
//             1536,              /* 每个消息的大小是 1 字节 */
//             sizeof(mqtt_message_rx_msq_pool),  /* 内存池的大小是 msg_pool 的大小 */
//             RT_IPC_FLAG_PRIO); /* 如果有多个线程等待，优先级大小的方法分配消息 */
//			 
//	mqtt_message_tx_sem = rt_sem_create("mqtt_message_tx_sem", 0, RT_IPC_FLAG_PRIO);
//	mqtt_message_rx_sem = rt_sem_create("mqtt_message_rx_sem", 0, RT_IPC_FLAG_PRIO);
//		if (mqtt_message_tx_sem == RT_NULL || mqtt_message_rx_sem == RT_NULL) {
//			for (;;) {
//				GPIO_SetPins(LED_ERR_PORT, LED_ERR_PIN);
//				DDL_DelayMS(100);
//				GPIO_ResetPins(LED_ERR_PORT, LED_ERR_PIN);
//				DDL_DelayMS(100);
//			}
//		}
//		get_tims = 0;
	// 三轴所有有效值数据
		usart_9_tx_app_buf[0] = 0x01;
		usart_9_tx_app_buf[1] = 0x03;
		usart_9_tx_app_buf[2] = 0x00;
		usart_9_tx_app_buf[3] = 0x00;
		usart_9_tx_app_buf[4] = 0x00;
		usart_9_tx_app_buf[5] = 0x11;
		usart_9_tx_app_buf[6] = 0x85;
		usart_9_tx_app_buf[7] = 0xC6;

	
  for (;;) {
//	  // 判断是否连接成功MQTT
//	  for (;;){
//		  if (mqtt1_status == 0){
//			  break;
//		  }else{
//			  rt_thread_mdelay(10);
//		  }
//	  }// 判断是否连接成功MQTT
	  
	  get_message = false;
	  // 读取三轴所有有效值数据
	  u9_rx_size = 0;
	  Thread_RS485_3_Usart_Send();
	  // 判断读取是否正确
	  if ( Thread_RS485_3_Usart_Recv() ){
		  if (u9_rx_size == 39 && usart_9_rx_app_buf[0] == 0x01 && usart_9_rx_app_buf[1] == 0x03){
			  get_message = true;
			  memset(xa_val_str, 0, 16);
			  memset(ya_val_str, 0, 16);
			  memset(za_val_str, 0, 16);
			  memset(xs_val_str, 0, 16);
			  memset(ys_val_str, 0, 16);
			  memset(zs_val_str, 0, 16);
			  memset(xk_val_str, 0, 16);
			  memset(yk_val_str, 0, 16);
			  memset(zk_val_str, 0, 16);
			  
			  sprintf(xa_val_str, "%d", (usart_9_rx_app_buf[8] * 0x100 + usart_9_rx_app_buf[9]) / 100);
			  sprintf(ya_val_str, "%d", (usart_9_rx_app_buf[20] * 0x100 + usart_9_rx_app_buf[21]) / 100);
			  sprintf(za_val_str, "%d", (usart_9_rx_app_buf[32] * 0x100 + usart_9_rx_app_buf[33]) / 100);
			  
			  sprintf(xs_val_str, "%d", (usart_9_rx_app_buf[12] * 0x100 + usart_9_rx_app_buf[13]) / 100);
			  sprintf(ys_val_str, "%d", (usart_9_rx_app_buf[24] * 0x100 + usart_9_rx_app_buf[25]) / 100);
			  sprintf(zs_val_str, "%d", (usart_9_rx_app_buf[36] * 0x100 + usart_9_rx_app_buf[37]) / 100);
			  
			  sprintf(xk_val_str, "%d", (usart_9_rx_app_buf[10] * 0x100 + usart_9_rx_app_buf[11]) / 100);
			  sprintf(yk_val_str, "%d", (usart_9_rx_app_buf[22] * 0x100 + usart_9_rx_app_buf[23]) / 100);
			  sprintf(zk_val_str, "%d", (usart_9_rx_app_buf[34] * 0x100 + usart_9_rx_app_buf[35]) / 100);
		  }
	  }
	  
	  if (get_message){
		  cJSON* root = cJSON_CreateObject();
		  cJSON* params = cJSON_CreateArray();
		  cJSON* x_g_object = cJSON_CreateObject();
		  cJSON* y_g_object = cJSON_CreateObject();
		  cJSON* z_g_object = cJSON_CreateObject();
		  cJSON* gnss_latitude_object = cJSON_CreateObject();
		  cJSON* gnss_latitude_type_object = cJSON_CreateObject();
		  cJSON* gnss_longitude_object = cJSON_CreateObject();
		  cJSON* gnss_longitude_type_object = cJSON_CreateObject();
		  cJSON* gnss_high_object = cJSON_CreateObject();
		  cJSON* gnss_high_unit_object = cJSON_CreateObject();
		  
		  cJSON_AddStringToObject(root, "messageId", "gnss_mems");
		  
		  // X轴加速度
		  cJSON_AddStringToObject(x_g_object, "key", "xa");
		  cJSON_AddStringToObject(x_g_object, "key", xa_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // Y轴加速度
		  cJSON_AddStringToObject(x_g_object, "key", "ya");
		  cJSON_AddStringToObject(x_g_object, "key", ya_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // Z轴加速度
		  cJSON_AddStringToObject(x_g_object, "key", "za");
		  cJSON_AddStringToObject(x_g_object, "key", za_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // X轴速度
		  cJSON_AddStringToObject(x_g_object, "key", "xs");
		  cJSON_AddStringToObject(x_g_object, "key", xs_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // Y加速度
		  cJSON_AddStringToObject(x_g_object, "key", "ys");
		  cJSON_AddStringToObject(x_g_object, "key", ys_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // Z加速度
		  cJSON_AddStringToObject(x_g_object, "key", "zs");
		  cJSON_AddStringToObject(x_g_object, "key", zs_val_str);
		  cJSON_AddItemToArray(params, x_g_object);
		  // 经度
//		  cJSON_AddStringToObject(x_g_object, "key", "longitude");
//		  cJSON_AddStringToObject(x_g_object, "key", gnss_longitude);
//		  cJSON_AddItemToArray(params, x_g_object);
//		  // 纬度
//		  cJSON_AddStringToObject(x_g_object, "key", "latitude");
//		  cJSON_AddStringToObject(x_g_object, "key", gnss_latitude);
//		  cJSON_AddItemToArray(params, x_g_object);
		  
		  cJSON_AddItemToArray(root, params);
		  
		  
		  char *jsonString = cJSON_Print(root);
		  
		  //rt_mq_send_wait(&mqtt_usart_tx_msq, jsonString, 1024, 500);
		  
		  cJSON_Delete(root);
		  free(jsonString);
	  }
	  
	// 休息10秒
	rt_thread_mdelay(10000);
  }  // 发送循环
} // 加速度仪任务函数


